package dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CacheRetrieveMode;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.Horder;
import model.Message;
import model.Strategy;
import model.Rorder;
import model.Route;
import model.View;
import model.Vroute;
import util.EntityManagerUtil;

public class StrategyDao {

	public static List<Strategy> getStrategys() {
		EntityManager em = EntityManagerUtil.getEntityManager();
		Query query = em.createQuery("SELECT c FROM Strategy c ");
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		List<Strategy> rnames = query.getResultList();
		em.close();
		return rnames;
	}

	public static boolean getStrategyNoCacheForBool(Strategy strategy) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Strategy c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Strategy newFind = new Strategy();
		try {
			newFind = (Strategy) query.setParameter("id", strategy.getId()).getSingleResult();
			em.close();
			return true;
		} catch (Exception e) {
			em.close();
			return false;
		}
	}

	public static Strategy getStrategyNoCache(Strategy strategy) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Strategy c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Strategy newFind = new Strategy();
		try {
			newFind = (Strategy) query.setParameter("id", strategy.getId()).getSingleResult();
			em.close();
			return newFind;
		} catch (Exception e) {
			em.close();
			return null;
		}
	}

	public static Strategy getStrategyNoCache(int nid) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Strategy c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Strategy newFind = new Strategy();
		try {
			newFind = (Strategy) query.setParameter("id", nid).getSingleResult();
			em.close();
			return newFind;
		} catch (Exception e) {
			em.close();
			return null;
		}
	}

	

	public static Strategy getStrategyNoCache(String name) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Strategy c where c.name=:name";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Strategy newFind = new Strategy();
		try {
			newFind = (Strategy) query.setParameter("name", name).getSingleResult();
			em.close();
			return newFind;
		} catch (Exception e) {
			em.close();
			return null;
		}
	}

	
	
	public static boolean delStrategy(Strategy strategy) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Strategy newFind = getStrategyNoCache(strategy);
			if (newFind != null) {
				Strategy delRorder = em.find(Strategy.class, strategy.getId());
				em.getTransaction().begin();
				em.remove(delRorder);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}

	public static boolean updateStrategy(Strategy strategy) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Strategy newFind = getStrategyNoCache(strategy);
			if (newFind != null) {
				Strategy upStrategy = em.find(Strategy.class, strategy.getId());
				em.getTransaction().begin();
				upStrategy.setInfo(strategy.getInfo());
				upStrategy.setName(strategy.getName());
				em.getTransaction().commit();
				em.close();

				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}
	
	public static boolean addStrategy(Strategy strategy) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
				em.getTransaction().begin();
				em.persist(strategy);
				em.getTransaction().commit();
				em.close();
				return true;
	
		} catch (Exception e) {

			return false;
		}
	}


	public static void main(String args[]) throws Exception {
		Strategy strategy = getStrategyNoCache("didi");
		System.out.println(strategy.getId());
	}

}
